//
//  DYSetVC.m
//  kafuMerchant
//
//  Created by 帝云科技 on 2018/9/21.
//  Copyright © 2018年 帝云科技. All rights reserved.
//

#import "DYSetVC.h"
#import "DYSetCell.h"
#import "DYFileCacheManager.h"

#import "DYUpdatePasswordVC.h"
#import "DYUpdatePasswordPhoneVC.h"
#import "DYUpdatePhoneVC.h"

@interface DYSetVC ()

@property (nonatomic,strong)NSString *currentVolum;
@property (nonatomic,strong)UIButton *exitButton;

@end

@implementation DYSetVC

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.navigationItem.title=@"设置";
    [self fileCache];
}
-(void)dy_initUI
{
    [super dy_initUI];
    
    self.tableView.backgroundColor=WhiteColor;
    
    [self.tableView registerClass:[DYSetCell class] forCellReuseIdentifier:@"DYSetCell"];
    
    [self.tableView addSubview:self.exitButton];
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 3;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return BILIHEIGHT(58);
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    DYSetCell *cell=[tableView dequeueReusableCellWithIdentifier:@"DYSetCell" forIndexPath:indexPath];
   
    NSArray *titleArray=@[@"手机号",@"登录密码",@"缓存"];
    NSArray *contentArray=@[@"15578837101",@"修改登录密码",@"清理缓存"];
    cell.titleLabel.text=titleArray[indexPath.row];
    cell.contentLabel.text=contentArray[indexPath.row];
    
    if (indexPath.row==2) {
        cell.rightLabel.text=self.currentVolum;
    }else{
        cell.rightLabel.text=@"";
    }
    
    return cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    if (indexPath.row==2) {
        UIAlertController *alert=[Utils alertActionCreateTitle:@"确定清除缓存？" andMassage:nil setActionEnterText:@"确定" setActionCancelText:@"取消" setEnterAction:^(UIAlertAction * _Nullable action) {
            [self cleanCache];
        }];
        UIAlertAction *cancel=alert.actions.firstObject;
        UIAlertAction *enter=alert.actions.lastObject;
        if (@available(iOS 8.4, *)) {
            [cancel setValue:[UIColor xhq_content] forKey:@"titleTextColor"];
            [enter setValue:[UIColor xhq_base] forKey:@"titleTextColor"];
        }
        [self presentViewController:alert animated:YES completion:nil];
    }else if (indexPath.row==1){
        [self modifyLoginPassword];
    }else{
        DYUpdatePhoneVC *vc=[[DYUpdatePhoneVC alloc]init];
        [self.navigationController pushViewController:vc animated:YES];
    }
}
-(void)modifyLoginPassword
{
    UIAlertController *alertVC=[UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:UIAlertControllerStyleActionSheet];
    [alertVC addAction:[UIAlertAction actionWithTitle:@"通过原登录密码修改" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        DYUpdatePasswordVC *vc=[[DYUpdatePasswordVC alloc]init];
        [self.navigationController pushViewController:vc animated:YES];
        
    }]];
    [alertVC addAction:[UIAlertAction actionWithTitle:@"通过手机验证码修改" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        DYUpdatePasswordPhoneVC *vc=[[DYUpdatePasswordPhoneVC alloc]init];
        [self.navigationController pushViewController:vc animated:YES];
    }]];
    
    [alertVC addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
        
    }]];
    
    if (@available(iOS 8.4, *)) {
        [alertVC.actions[0] setValue:XHQHexColor(0x6D7075) forKey:@"titleTextColor"];
        [alertVC.actions[1] setValue:XHQHexColor(0x6D7075) forKey:@"titleTextColor"];
        [alertVC.actions[2] setValue:[UIColor xhq_base] forKey:@"titleTextColor"];
    }
    
    [self presentViewController:alertVC animated:YES completion:nil];
}
#pragma mark - 计算缓存
- (void)fileCache
{
    [DYFileCacheManager cacheCompletion:^(NSString *text) {
        self.currentVolum = text;
        [self.tableView reloadData];
    }];
}
#pragma mark - 清除缓存
- (void)cleanCache
{
    XHQHUDSHOW(self.view);
    [DYFileCacheManager clearCacheComption:^{
        XHQHUDHIDE(self.view);
        XHQHUDTEXT(@"清除成功");
        [self fileCache];
    }];
}
#pragma mark - 退出登录
-(void)exitButtonClick
{
    XHQHUDTEXT(@"退出登录");
}
-(UIButton *)exitButton
{
    if (!_exitButton) {
        _exitButton=[Utils createBtnWithType:UIButtonTypeCustom backgroundColor:XHQHexColor(0xF75151) action:@selector(exitButtonClick) target:self title:@"退出登录" image:nil font:17 textColor:WhiteColor];
        [_exitButton xhq_cornerRadius:BILIHEIGHT(19)];
        _exitButton.frame=CGRectMake((kScreenWidth-BILIWIDTH(343))/2, kScreenHeight-kNavigationStatusHeight-BILIHEIGHT(90), BILIWIDTH(343), BILIHEIGHT(38));
    }
    return _exitButton;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
